package com.net.shop.car.manager.ui.personalcenter.share;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

import com.net.shop.car.manager.R;
import com.net.shop.car.manager.api.model.ShareRule;
import com.net.shop.car.manager.api.volley.VolleyCenter;
import com.net.shop.car.manager.api.volley.VolleyListenerImpl;
import com.net.shop.car.manager.api.volley.request.GetShareRuleList;
import com.net.shop.car.manager.api.volley.response.ShareRuleListResponse;
import com.net.shop.car.manager.ui.FullScreenDialog;

/**
 * @author Deve
 * 
 */
public class ShareDialog extends FullScreenDialog {

	private GridView appGridView;
	private ListView rulesListView;

	private String[] strs = new String[] {
			"朋友圈","QQ好友","微博","微信个人","空间","贴吧"
	};

	private int[] drawables = new int[] {
		R.drawable.skyblue_logo_wechatmoments_checked,
		R.drawable.skyblue_logo_qq_checked,
		R.drawable.skyblue_logo_sinaweibo_checked,	
		R.drawable.skyblue_logo_wechat_checked,
		R.drawable.skyblue_logo_qzone_checked,
		R.drawable.skyblue_logo_baidutieba_checked
	};

	public ShareDialog(Context context) {
		super(context, R.style.Dialog_Fullscreen);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.settings_share);

		initViews();
	}

	/**
	 * 
	 */
	private void initViews() {
		initTitle(findViewById(R.id.share_title), activity.getResources()
				.getString(R.string.personal_share_youli));
		appGridView = (GridView) findViewById(R.id.share_gridview);
		
		rulesListView = (ListView) findViewById(R.id.share_rule_list);

		setAppinfo();

		setRulesList();
	}

	private void setRulesList() {
		VolleyCenter.getVolley().addGetRequest(
				new GetShareRuleList(),
				new VolleyListenerImpl<ShareRuleListResponse>(
						new ShareRuleListResponse()) {

					@Override
					public void deleveryResponse(ShareRuleListResponse response) {
						if (response.isSuccess()) {
							rulesListView.setAdapter(new ShareRuleAdapter(
									response.getRules()));
						}
					}

				});
	}

	private class ShareRuleAdapter extends BaseAdapter {

		private List<ShareRule> rules;

		public ShareRuleAdapter(List<ShareRule> rules) {
			this.rules = rules;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return null == rules ? 0 : rules.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View currentView = convertView;
			if (currentView == null) {
				currentView = activity.inflater.inflate(
						R.layout.sharerule_listitem, parent, false);
				currentView.setTag(new RuleViewHolder(currentView));
			}
			RuleViewHolder ruleViewHolder = (RuleViewHolder) currentView
					.getTag();
			ShareRule shareRule = rules.get(position);
			ruleViewHolder.name.setText(shareRule.getShareName());
			ruleViewHolder.vb.setText(shareRule.getShareVB() + "分");
			return currentView;
		}

		private class RuleViewHolder {
			TextView name;
			TextView vb;

			public RuleViewHolder(View view) {
				name = (TextView) view.findViewById(R.id.sharerule_name);
				vb = (TextView) view.findViewById(R.id.sharerule_vb);
			}
		}
	}

	private void setAppinfo() {
		final List<AppInfo> appInfos = getShareAppList();

		appGridView.setAdapter(new BaseAdapter() {

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				View currentView = convertView;
				AppInfo appInfo = appInfos.get(position);
				if (currentView == null) {
					currentView = activity.inflater.inflate(
							R.layout.share_app_icon, parent, false);
				}
				float witdh = activity.getResources().getDimension(
						R.dimen.img_icon_witdh);
				TextView textView = (TextView) currentView;
				textView.setText(appInfo.getAppName());
				appInfo.getAppIcon().setBounds(
						new Rect(0, 0, (int) witdh, (int) witdh));
				textView.setCompoundDrawables(null, appInfo.getAppIcon(), null,
						null);
				return currentView;
			}

			@Override
			public long getItemId(int position) {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public Object getItem(int position) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return appInfos.size();
			}
		});

		appGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				showShare(activity);
			}
		});
	}

	private List<AppInfo> getShareAppList() {
		List<AppInfo> shareAppInfos = new ArrayList<AppInfo>();

		for (int i = 0; i < strs.length; i++) {
			AppInfo appInfo = new AppInfo();
			appInfo.setAppName(strs[i]);
			appInfo.setAppIcon(activity.getResources()
					.getDrawable(drawables[i]));
			shareAppInfos.add(appInfo);
		}
		return shareAppInfos;
	}

	public class AppInfo {

		private String AppPkgName;
		private String AppName;
		private Drawable AppIcon;

		public AppInfo() {
			super();
		}

		public AppInfo(String appPkgName) {
			super();
			AppPkgName = appPkgName;
		}

		public String getAppPkgName() {
			return AppPkgName;
		}

		public void setAppPkgName(String appPkgName) {
			AppPkgName = appPkgName;
		}

		public String getAppName() {
			return AppName;
		}

		public void setAppName(String appName) {
			AppName = appName;
		}

		public Drawable getAppIcon() {
			return AppIcon;
		}

		public void setAppIcon(Drawable appIcon) {
			AppIcon = appIcon;
		}

	}

	private void showShare(final Context context) {
		// TODO Auto-generated method stub
		ShareSDK.initSDK(context);
		OnekeyShare oks = new OnekeyShare();
		// 关闭sso授权
		oks.disableSSOWhenAuthorize();

		// 分享时Notification的图标和文字
		oks.setNotification(R.drawable.ic_launcher,
				activity.getString(R.string.app_name));
		// title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
		oks.setTitle(activity.getString(R.string.share));
		// titleUrl是标题的网络链接，仅在人人网和QQ空间使用
		oks.setTitleUrl("http://api.clejw.com/apk/jxcl.apk");
		// text是分享文本，所有平台都需要这个字段
		oks.setText("免费查违章，五元洗车，找实惠，找周边请上车联网。http://api.clejw.com/apk/jxcl.apk");
		// // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
		oks.setImagePath("");
		// oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/05/21/oESpJ78_533x800.jpg");
		// url仅在微信（包括好友和朋友圈）中使用
		oks.setUrl("http://api.clejw.com/apk/jxcl.apk");
		// comment是我对这条分享的评论，仅在人人网和QQ空间使用
		oks.setComment("免费查违章，五元洗车，找实惠，找周边请上车联网。http://api.clejw.com/apk/jxcl.apk");
		// site是分享此内容的网站名称，仅在QQ空间使用
		oks.setSite(activity.getString(R.string.app_name));
		// siteUrl是分享此内容的网站地址，仅在QQ空间使用
		oks.setSiteUrl("http://api.clejw.com/apk/jxcl.apk");

		// 启动分享GUI
		oks.show(context);
	}
}
